<template>
  <el-dialog :modal="false" :title="title" :visible.sync="dialogVisible" width="550px" :before-close="handleClose">
    <div class="userselect">
      <section class="left">
        <div class="search">
          <el-input v-model="depName" placeholder="请输入部门名称"> </el-input>
          <div class=" searchicon el-icon-search" @click="seachMan()"></div>
        </div>
        <div class="tree">
          <el-radio-group v-model="depRadio">
            <el-tree
              ref="tree"
              :data="treedata"
              :props="defaultProps"
              node-key="deptId"
              :default-expanded-keys="defDeptId"
              :filter-node-method="filterNode"
              :highlight-current="true"
              @node-click="handleNodeClick"
            >
              <span slot-scope="{ node, data }" class="custom-tree-node">
                <span v-if="data.name !== '中储南京智慧物流科技有限公司'" class="tree-list">
                  <el-radio :label="data.deptId">
                    {{ node.label }}
                  </el-radio>
                </span>
                <p v-if="data.name == '中储南京智慧物流科技有限公司'" style="font-size:14px">{{ data.name }}</p>
              </span>
            </el-tree>
          </el-radio-group>
        </div>
      </section>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose()">取 消</el-button>
      <el-button size="mini" type="primary" @click="getdata()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { all } from '@/api/userselect'

export default {
  props: {
    title: {
      type: String,
      default: '部门选择'
    },
    deptId: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      depRadio: '',
      depName: '',
      treedata: [],
      defaultProps: {
        children: 'list',
        label: 'name'
      },
      defDeptId: [],
      dialogVisible: true,
      selecteduserlist: null
    }
  },
  computed: {},
  watch: {},
  mounted() {
    this.defDeptId = this.deepClone(this.deptId)
    this.depRadio = this.defDeptId[0]
    this.fetchall()
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    seachMan() {
      this.$refs.tree.filter(this.depName)
    },
    fetchall() {
      all()
        .then(res => {
          const resdata = this.deepClone([res.data])
          this.treedata = resdata
        })
        .catch(() => {
          this.loading = false
        })
    },
    getdata() {
      this.$emit('getdata', this.selecteduserlist)
      this.handleClose()
    },
    handleNodeClick(data) {
      this.depRadio = data.deptId
      this.selecteduserlist = this.deepClone(data)
      this.defDeptId = []
    },
    handleClose(done) {
      this.$emit('closeDialog', false)
      done && done()
    }
  }
}
</script>

<style lang="scss" scoped>
.search {
  display: flex;
  position: relative;
  margin-bottom: 10px;
  .searchicon {
    font-size: 18px;
    line-height: 32px;
    right: 0px;
    width: 32px;
    position: absolute;
    cursor: pointer;
  }
}
.userselect {
  .left {
    background: #f8f8f8;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    padding: 10px;

    .tree {
      width: 100%;
      padding: 15px;
      height: 300px;
      overflow-y: auto;
      background: #ffffff;
      border-radius: 6px;
      border: 1px solid #d9d9d9;
      margin-bottom: 10px;
    }
    .tree-list {
      display: flex;
      line-height: 16px;
    }
    .ismenu {
      width: 16px;
      height: 16px;
      // margin: 0 10px 0 5px;
      // background: url('icon/org.png');
      // background-size: cover;
    }
    .isunit {
      width: 16px;
      height: 16px;
      // margin: 0 10px 0 5px;
      // background: url('icon/member.png');
      // background-size: cover;
    }
  }
}
.width300 {
  width: 300px;
}
::v-deep {
  .el-button {
    width: 120px;
  }
}
.row {
  display: flex;
}
</style>
